package com.easyjop.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.easyjop.entity.enums.CategoryTypeEnum;
import com.easyjop.entity.enums.ResponseCodeEnum;
import com.easyjop.entity.po.ExamQuestion;
import com.easyjop.entity.po.QuestionInfo;
import com.easyjop.entity.query.ExamQuestionQuery;
import com.easyjop.entity.query.QuestionInfoQuery;
import com.easyjop.exception.BusinessException;
import com.easyjop.mappers.ExamQuestionMapper;
import com.easyjop.mappers.QuestionInfoMapper;
import com.easyjop.service.QuestionInfoService;
import org.springframework.stereotype.Service;

import com.easyjop.entity.enums.PageSize;
import com.easyjop.entity.query.CategoryQuery;
import com.easyjop.entity.po.Category;
import com.easyjop.entity.vo.PaginationResultVO;
import com.easyjop.entity.query.SimplePage;
import com.easyjop.mappers.CategoryMapper;
import com.easyjop.service.CategoryService;
import com.easyjop.utils.StringTools;
import org.springframework.transaction.annotation.Transactional;


/**
 * 分类 业务接口实现
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

	@Resource
	private CategoryMapper<Category, CategoryQuery> categoryMapper;
	@Resource
	private QuestionInfoMapper<QuestionInfo, QuestionInfoQuery> questionInfoMapper;
	@Resource
	private ExamQuestionMapper<ExamQuestion, ExamQuestionQuery> examQuestionMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<Category> findListByParam(CategoryQuery param) {
		return this.categoryMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(CategoryQuery param) {
		return this.categoryMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<Category> findListByPage(CategoryQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<Category> list = this.findListByParam(param);
		PaginationResultVO<Category> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(Category bean) {
		return this.categoryMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<Category> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.categoryMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<Category> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.categoryMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(Category bean, CategoryQuery param) {
		StringTools.checkParam(param);
		return this.categoryMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(CategoryQuery param) {
		StringTools.checkParam(param);
		return this.categoryMapper.deleteByParam(param);
	}

	/**
	 * 根据CategoryId获取对象
	 */
	@Override
	public Category getCategoryByCategoryId(Integer categoryId) {
		return this.categoryMapper.selectByCategoryId(categoryId);
	}

	/**
	 * 根据CategoryId修改
	 */
	@Override
	public Integer updateCategoryByCategoryId(Category bean, Integer categoryId) {
		return this.categoryMapper.updateByCategoryId(bean, categoryId);
	}

	/**
	 * 根据CategoryId删除
	 */
	@Override
	public Integer deleteCategoryByCategoryId(Integer categoryId) {
		QuestionInfoQuery questionInfoQuery = new QuestionInfoQuery();
		questionInfoQuery.setCategoryId(categoryId);
		Integer count = questionInfoMapper.selectCount(questionInfoQuery);
		if (count > 0) {
			throw new BusinessException("问题库正在使用改分类无法删除");
		}
		ExamQuestionQuery examQuestionQuery = new ExamQuestionQuery();
		examQuestionQuery.setCategoryId(categoryId);
		count = examQuestionMapper.selectCount(examQuestionQuery);
		if (count > 0) {
			throw new BusinessException("试题库正在使用改分类无法删除");
		}
		return this.categoryMapper.deleteByCategoryId(categoryId);
	}

    @Override
	@Transactional(rollbackFor = Exception.class)
    public void saveCategory(Category category, Integer bankId) {
		/*新增*/
      if (category.getCategoryId()==null){
		  CategoryQuery query = new CategoryQuery();
		  Integer count = categoryMapper.selectCount(query);
		  category.setSort(count+1);
		  category.setType(bankId);
		  this.categoryMapper.insert(category);
	  }else {
		  category.setType(bankId);
		  CategoryQuery query = new CategoryQuery();
		  query.setCategoryName(category.getCategoryName());
		  query.setType(bankId);
		  Integer count = this.categoryMapper.selectCount(query);
		  if (count>1){
			  throw new BusinessException("分类名字已存在");
		  }
		  query =new CategoryQuery();
		  query.setCategoryId(category.getCategoryId());
		  List<Category> categories = this.categoryMapper.selectList(query);
		  Category category1 = categories.get(0);
		  /*更新分类信息*/
		  this.categoryMapper.updateByCategoryId(category,category.getCategoryId());
		  if (!category.getCategoryName().equals(category1.getCategoryName())){
			  this.categoryMapper.updateCategoryName(category.getCategoryName(),category.getCategoryId());
		  }
	  }

	}

	@Override
	public void changeSort(String categoryIds) {
		String[] sortStr=categoryIds.split(",");
		Integer index=1;
		for (String str : sortStr) {
			Category category = new Category();
			category.setSort(index++);
			this.categoryMapper.updateByCategoryId(category,Integer.valueOf(str));
		}
	}

	@Override
	public List<Category> loadAllCategoryByQuery(Integer bankId) {
		CategoryTypeEnum byType = CategoryTypeEnum.getByType(bankId);
		if (byType==null){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		CategoryQuery query = new CategoryQuery();
		query.setOrderBy("sort asc");
		query.setTypes(new Integer[]{byType.getType()});
		List<Category> categories = this.categoryMapper.selectList(query);
		return categories;
	}


	@Override
	public List<Category> loadAllCategoryByType(Integer type) {
		CategoryQuery query = new CategoryQuery();
		query.setType(type);
		query.setOrderBy("sort Asc");
		List<Category> listByParam = this.findListByParam(query);
		return listByParam;
	}
}